﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Warehouse.Web.AppCode;
using System.Data.SqlClient;
using System.Web.Script.Serialization;

namespace Warehouse.Web.Management
{
    public partial class Distributors : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        protected override void ProcessAjaxRequest(HttpContext context, string action)
        {
            switch (action)
            {
                case "Get":
                    {
                        int DistributorID = int.Parse(context.Request.QueryString["DistributorID"]);
                        DataAccess.Distributor distributor = Warehouse.DataAccess.Distributor.GetDistributor(DistributorID);
                        if (distributor != null)
                        {
                            WebUtil.WriteJsonResult(context, distributor);
                        }
                        else
                        {
                            WebUtil.WriteJsonError(context, "客户已经被删除");
                        }
                    }
                    break;
                case "Add":
                    {
                        string Name = context.Request.Form["Name"].Trim();
                        string Code = context.Request.Form["Code"].Trim();
                        string PostAddress = context.Request.Form["PostAddress"];
                        string PostCode = context.Request.Form["PostCode"];
                        string Consignee = context.Request.Form["Consignee"];
                        string Telephone = context.Request.Form["Telephone"];
                        string Email = context.Request.Form["Email"];
                        int DefaultHouseID = 0;
                        if (!int.TryParse(context.Request.Form["DefaultHouseID"], out DefaultHouseID))
                        {
                            DefaultHouseID = 0;
                        }

                        if (string.IsNullOrEmpty(Name))
                        {
                            WebUtil.WriteJsonError(context, "公司名不能为空");
                            return;
                        }
                        try
                        {
                            DataAccess.Distributor distributor = new Warehouse.DataAccess.Distributor()
                            {
                                Name = Name,
                                Code = Code,
                                PostAddress = PostAddress,
                                PostCode = PostCode,
                                Consignee = Consignee,
                                Telephone = Telephone,
                                Email = Email,
                                DefaultHouseID = DefaultHouseID
                            };
                            distributor.Save();
                            WebUtil.WriteJsonResult(context, null);
                        }
                        catch (SqlException ex)
                        {
                            if (ex.Number == 2601)
                            {
                                WebUtil.WriteJsonError(context, "该公司名已存在，请更改名称后再保存");
                            }
                            else
                            {
                                throw;
                            }
                        }
                    }
                    break;
                case "Save":
                    {
                        int DistributorID = int.Parse(context.Request.Form["ID"]);
                        string Name = context.Request.Form["Name"].Trim();
                        string Code = context.Request.Form["Code"].Trim();
                        string PostAddress = context.Request.Form["PostAddress"];
                        string PostCode = context.Request.Form["PostCode"];
                        string Consignee = context.Request.Form["Consignee"];
                        string Telephone = context.Request.Form["Telephone"];
                        string Email = context.Request.Form["Email"];
                        int DefaultHouseID = 0;
                        if (!int.TryParse(context.Request.Form["DefaultHouseID"], out DefaultHouseID))
                        {
                            DefaultHouseID = 0;
                        }

                        if (string.IsNullOrEmpty(Name))
                        {
                            WebUtil.WriteJsonError(context, "公司名不能为空");
                            return;
                        }
                        try
                        {
                            DataAccess.Distributor distributor = Warehouse.DataAccess.Distributor.GetDistributor(DistributorID);
                            if (distributor != null)
                            {
                                distributor.Name = Name;
                                distributor.Code = Code;
                                distributor.PostAddress = PostAddress;
                                distributor.PostCode = PostCode;
                                distributor.Consignee = Consignee;
                                distributor.Telephone = Telephone;
                                distributor.Email = Email;
                                distributor.DefaultHouseID = DefaultHouseID;

                                distributor.Save();
                                WebUtil.WriteJsonResult(context, null);
                            }
                            else
                            {
                                WebUtil.WriteJsonError(context, "客户已经被删除");
                            }
                        }
                        catch (SqlException ex)
                        {
                            if (ex.Number == 2601)
                            {
                                WebUtil.WriteJsonError(context, "该公司名已存在，请更改名称后再保存");
                            }
                            else
                            {
                                throw;
                            }
                        }
                    }
                    break;
                case "Delete":
                    {
                        int DistributorID = int.Parse(context.Request.Form["DistributorID"]);
                        DataAccess.Distributor distributor = Warehouse.DataAccess.Distributor.GetDistributor(DistributorID);
                        if (distributor != null)
                        {
                            try
                            {
                                distributor.Delete();
                                WebUtil.WriteJsonResult(context, null);
                            }
                            catch (SqlException ex)
                            {
                                if (ex.Number == 547)
                                {
                                    WebUtil.WriteJsonError(context, "该客户已有出库记录，不能删除");
                                }
                                else
                                {
                                    throw;
                                }
                            }
                        }
                        else
                        {
                            WebUtil.WriteJsonResult(context, null);
                        }
                    }
                    break;
                default:
                    base.ProcessAjaxRequest(context, action);
                    break;
            }
        }

        void LoadData()
        {
            var houses = DataAccess.Warehouse.GetWarehouses();
            this.rptDistributors.DataSource = DataAccess.Distributor.GetDistributors().Select(p =>
            {
                DataAccess.Warehouse h = houses.SingleOrDefault(arg => arg.ID == p.DefaultHouseID);
                return new
                {
                    Distributor = p,
                    DefalutHouse = h ?? new DataAccess.Warehouse()
                };
            });
            this.rptDistributors.DataBind();
            this.ltAllHouses.Text = string.Format("<script type=\"text/javascript\" id=\"allhouses\">{0}</script>", new JavaScriptSerializer().Serialize(houses.Select(p => new
            {
                ID = p.ID,
                Name = p.Name
            })));
        }
    }
}
